## Replication Files for System Justification in Authoritarian Regimes 
## Figure 1: Egypt's Authoritarianism in Comparative Perspective

setwd("")

library(readxl)
library(dplyr)
library(ggplot2)
library(tidyverse)
library(readxl)
library(foreign)
library(janitor)
library(lubridate)

vdem <- read.csv(file = "vdem_full.csv")

vdem_filter <-
  vdem %>%
  filter(year >= 1980) %>%
  mutate(
    regime_type = case_when(
      v2x_regime == 0 ~ "Authoritarian regimes",
      v2x_regime == 1 ~ "Authoritarian regimes",
      v2x_regime == 2 ~ "Democracies",
      v2x_regime == 3 ~ "Democracies")) %>%
  mutate(
    regime_type2 = case_when(
      country_name == "Egypt" ~ "Egypt",
      v2x_regime == 0 ~ "Authoritarian regimes",
      v2x_regime == 1 ~ "Authoritarian regimes",
      v2x_regime == 2 ~ "Democracies",
      v2x_regime == 3 ~ "Democracies")) %>%
  mutate(
    regime_type3 = case_when(
      country_name == "Egypt" ~ "Egypt",
      v2x_regime == 0 ~ "Closed autocracy",
      v2x_regime == 1 ~ "Electoral autocracy",
      v2x_regime == 2 ~ "Electoral democracy",
      v2x_regime == 3 ~ "Liberal democracy")) %>%
  dplyr::select(country_name,year,regime_type,regime_type2,regime_type3,v2x_clphy,v2x_clpol)

vdem_filter <-
  vdem_filter %>%
  dplyr::mutate(
    regime_type2 = factor( ## REORDER LEVELS
      regime_type2, levels = c("Democracies", "Authoritarian regimes", "Egypt")))


vdem_filter %>%
  filter(!is.na(regime_type2)) %>%
  filter(!(country_name == "Egypt" & year >= 2020)) %>%
  filter(!(country_name == "Egypt" & year < 1980)) %>%
  ggplot(aes(x = year, y = v2x_clphy, color = regime_type2, shape = regime_type2)) +
  geom_jitter(aes(size=regime_type2),alpha = 0.15) + 
  geom_smooth(method = "loess", se = F) +
  stat_summary(fun.data = mean_sdl, geom = "ribbon", alpha = 0.1, fun.args = list(mult = 0.5)) +
  guides(colour = guide_legend(override.aes = list(alpha = 1,fill=NA))) +
  theme_bw() +
  labs(x = "Year",
       y = "Freedom from physical violence",
       title = "") +
  scale_y_continuous(
    breaks=seq(0.0, 1.0, 0.1),
    minor_breaks = NULL,
    limits = c(0.0, 1.0),
    expand=c(0, 0)) +
  scale_x_continuous(
    breaks=seq(1980, 2020, 5),
    minor_breaks = NULL,
    limits = c(1979, 2021),
    expand=c(0, 0)) +
  scale_shape_manual(values = c(4, 1,20), name = "") +
  scale_color_manual(values = c("black", "darkgray", "red"), name = "") +
  scale_size_manual(values = c(1, 1, 0), name = "") +
  theme(
    legend.position = "bottom", # place legend on bottom of plot
    legend.key.width = unit(1, "cm"), # set length of color bars in legend
    # legend.text = element_text(margin = margin(r = .01, unit = 'pt'), size = 10), # set space between items in legend
    legend.margin = margin(-10, 0, 0, 0), # set margins
    legend.title = element_blank(), # remove legend title
    plot.title = element_text(hjust = 0.5), # center title
    text = element_text(size = 12), # set font size
    axis.text.y = element_text(size = 12), # set font size for y-axis tick labels
    axis.text.x = element_text(size = 12), # set font size for y-axis labels
    panel.grid.minor = element_blank(), # no minor grid for x-axis and y-axis
    panel.grid.major.x = element_blank(), # no major grid for x-axis 
    axis.line = element_line(colour = "black"))  # set color for x-axis and y-axis line

vdem_filter %>%
  filter(!is.na(regime_type2)) %>%
  filter(!(country_name == "Egypt" & year >= 2020)) %>%
  filter(!(country_name == "Egypt" & year < 1980)) %>%
  ggplot(aes(x = year, y = v2x_clpol, color = regime_type2, shape = regime_type2)) +
  geom_jitter(aes(size=regime_type2),alpha = 0.15) + 
  geom_smooth(method = "loess", se = F) +
  stat_summary(fun.data = mean_sdl, geom = "ribbon", alpha = 0.1, fun.args = list(mult = 0.5)) +
  guides(colour = guide_legend(override.aes = list(alpha = 1,fill=NA))) +
  theme_bw() +
  labs(x = "Year",
       y = "Respect for political civil liberties",
       title = "") +
  scale_y_continuous(
    breaks=seq(0.0, 1.0, 0.1),
    minor_breaks = NULL,
    limits = c(0.0, 1.0),
    expand=c(0, 0)) +
  scale_x_continuous(
    breaks=seq(1980, 2020, 5),
    minor_breaks = NULL,
    limits = c(1979, 2021),
    expand=c(0, 0)) +
  scale_shape_manual(values = c(4, 1,20), name = "") +
  scale_color_manual(values = c("black", "darkgray", "red"), name = "") +
  scale_size_manual(values = c(1, 1, 0), name = "") +
  theme(
    legend.position = "bottom", # place legend on bottom of plot
    legend.key.width = unit(1, "cm"), # set length of color bars in legend
    # legend.text = element_text(margin = margin(r = .01, unit = 'pt'), size = 10), # set space between items in legend
    legend.margin = margin(-10, 0, 0, 0), # set margins
    legend.title = element_blank(), # remove legend title
    plot.title = element_text(hjust = 0.5), # center title
    text = element_text(size = 12), # set font size
    axis.text.y = element_text(size = 12), # set font size for y-axis tick labels
    axis.text.x = element_text(size = 12), # set font size for y-axis labels
    panel.grid.minor = element_blank(), # no minor grid for x-axis and y-axis
    panel.grid.major.x = element_blank(), # no major grid for x-axis 
    axis.line = element_line(colour = "black"))  # set color for x-axis and y-axis line

## Figure 2: Uncertainty Avoidance Index (UAI) in Comparative Perspective

setwd("C:/Users/enugent/Dropbox/Book Project_2/Writing_Article/Personality and Propaganda/Personality Paper/JoP/R&R")
library(readxl)

uai <- read_excel("uai.xlsx")

abb <- c(uai$ctr)
scores <- c(uai$uai)

scoresAW<- c(68, 70, 64, 66, 57, 96, 55, 67, 68)
labelsAW <- c("Arab Cluster, 1967", "Kuwait", "Saudi Arabia", "UAE", "Lebanon", "Iraq", "Egypt", "Libya", "Morocco")

xticks <- c("Low", "20", "40", "60", "80", "100", "High")

x <- list("Other" = scores, "Arab World" = scoresAW)

stripchart(x,
           main="Uncertainty Avoidance Index (UAI) Scores, Arab World vs. Other",
           xlab="UAI Score",
           ylab="Group",
           method="jitter",
           col=c("orange","darkgreen"),
           pch=16, 
           xaxt = 'n'
)
xlabels <- c("Low", "20", "40", "60", "80", "100", "High")
axis(1,  at= c(5, 20, 40, 60, 80, 100, 115), labels=xlabels)

text(55, 1.85, labels="Egypt", cex=.75)
text(68, 1.85, labels="Arab Cluster, 1967", cex=.75)


